package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.PlayRecord;
import com.elinkway.infinitemovies.bean.PlayRecordList;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * the parser for playrecord
 */
public class UserPlayRecordParser extends LVideoMobileParser<PlayRecordList> {
    @Override
    public PlayRecordList parse(JSONObject data) throws Exception {
        PlayRecordList playRecordList = null;
        if(data != null){
            playRecordList = new PlayRecordList();
            String code = super.getCode();
            String msg = super.getMsg();
            playRecordList.setTotal(data.optInt("total"));
            if("200".equals(code)){
                JSONArray dataArray = data.optJSONArray("data");
                if(dataArray != null && dataArray.length() > 0){
                    List<PlayRecord> list = new ArrayList<PlayRecord>();
                    PlayRecord record = null;
                    JSONObject playObj = null;
                    for (int i = 0; i < dataArray.length(); i++){
                        playObj = dataArray.optJSONObject(i);
                        if(playObj != null){
                            record = new PlayRecord();
                            record.setAid(playObj.optString("aid"));
                            record.setName(playObj.optString("name"));
                            record.setSite(playObj.optString("source"));
                            record.setVid(playObj.optString("vid"));
                            record.setSeekHistory(playObj.optLong("playTime") * 1000);
                            record.setTime(playObj.optLong("updateTime"));
                            record.setEpisodeName(playObj.optString("episodes"));
                            record.setPorder(playObj.optString("episodes"));
                            record.setVt(playObj.optString("vt"));
                            record.setPoster(playObj.optString("poster"));
                            record.setIsUpload("1");
                            list.add(record);
                            record = null;
                            playObj = null;
                        }
                    }
                    playRecordList.setPlayRecordList(list);
                }
            }
        }
        return playRecordList;
    }
}
